//
//  STStoryService.swift
//  Story
//
//  Created by 汤泽川 on 2019/4/19.
//  Copyright © 2019 汤泽川. All rights reserved.
//

import Foundation
import RxSwift
import PKHUD
import Alamofire

class STStoryService {
    @discardableResult
    public func fetchStoryInfo(channelId : Int, page : Int, count : Int) -> STResponse<ResponseModel<[STStoryInfo]> > {
        let response = STResponse<ResponseModel<[STStoryInfo]> >()
        STStoryApi.fetchStoryList(channelId: channelId, page: page, count: count) { (data, error) in
            response.execute(data, error)
        }
        return response
    }
    @discardableResult
    public func fetchStoryCategotyInfo() -> STResponse<[STStoryCategoryModel]> {
        let response = STResponse<[STStoryCategoryModel]>()
        STStoryApi.fetchStoryCategory { (data, error) in
            if let data = data {
                response.execute(data.data, error)
            } else {
                response.execute(nil, error)
            }
        }
        return response
    }
    @discardableResult
    public func searchStoryInfo(keyword : String, page : Int, count : Int) -> STResponse<ResponseModel<[STStoryInfo]> > {
        let response = STResponse<ResponseModel<[STStoryInfo]> >()
        STStoryApi.searchStoryList(keyword: keyword, page: page, count: count) { (data, error) in
            response.execute(data, error)
        }
        return response
    }
    
    public func searchAuthorStoryInfo(authorName : String, page : Int, count : Int) -> STResponse<ResponseModel<[STStoryInfo]> > {
        let response = STResponse<ResponseModel<[STStoryInfo]> >()
        STStoryApi.searchAuthorStoryList(authorName: authorName, page: page, count: count) { (data, error) in
            response.execute(data, error)
        }
        return response
    }
    
    let disposeBag = DisposeBag()
    
    @discardableResult
    public func uploadStoryInfo(title : String, categoryId : Int, image : Data?, content : String) -> STResponse<ResponseModel<String> > {
        let response = STResponse<ResponseModel<String> >()

        let imageObserver = PublishSubject<String?>()
        let contentObserver = PublishSubject<String>()

        _ = Observable.zip(imageObserver, contentObserver).subscribe(onNext: { (result) in
            STStoryApi.publishStory(storyName: title, introduce: String(content.prefix(20)), channelId: categoryId, storyUrl: result.1, avatarUrl: result.0, result: { (result, error) in
                response.execute(result, error)
            })
        })
        
        let uploadService = STUploadService()
        
        uploadService.uploadTxt(fileData: content.data(using: .utf8)!).result { (url, error) in
            if url != nil {
                contentObserver.onNext(url!)
            }
            if error != nil {
                response.execute(nil, error)
            }
        }
        if image != nil {
            uploadService.uploadImage(fileData: image!).result { (url, error) in
                if url != nil {
                    imageObserver.onNext(url!)
                }
                if error != nil {
                    response.execute(nil, error)
                }
            }
        } else {
            imageObserver.onNext(nil)
        }
        return response
    }
}
